
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 06/20/2013 13:53:57
-- Generated from EDMX file: E:\ZZLProject\losezhang\DDD从零起步\Domain.Core.Entities\Model1.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [background];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Order_Detail_Order_Info]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Order_Detail] DROP CONSTRAINT [FK_Order_Detail_Order_Info];
GO
IF OBJECT_ID(N'[dbo].[FK_Product_User_Info]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Product] DROP CONSTRAINT [FK_Product_User_Info];
GO
IF OBJECT_ID(N'[dbo].[FK_ProductDetail_Product]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProductDetail] DROP CONSTRAINT [FK_ProductDetail_Product];
GO
IF OBJECT_ID(N'[dbo].[FK_WebDepartments_WebDepartments]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WebDepartments] DROP CONSTRAINT [FK_WebDepartments_WebDepartments];
GO
IF OBJECT_ID(N'[dbo].[FK_WebManageMenus_WebManageMenus]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WebManageMenus] DROP CONSTRAINT [FK_WebManageMenus_WebManageMenus];
GO
IF OBJECT_ID(N'[dbo].[FK_WebManageRole_WebManageMenus_WebManageMenus1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WebManageRole_WebManageMenus] DROP CONSTRAINT [FK_WebManageRole_WebManageMenus_WebManageMenus1];
GO
IF OBJECT_ID(N'[dbo].[FK_WebManageRole_WebManageMenus_WebManageRoles1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WebManageRole_WebManageMenus] DROP CONSTRAINT [FK_WebManageRole_WebManageMenus_WebManageRoles1];
GO
IF OBJECT_ID(N'[dbo].[FK_WebManageRoles_WebDepartments1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WebManageRoles] DROP CONSTRAINT [FK_WebManageRoles_WebDepartments1];
GO
IF OBJECT_ID(N'[dbo].[FK_WebManageUser_WebManageRoles_WebManageRoles1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WebManageUser_WebManageRoles] DROP CONSTRAINT [FK_WebManageUser_WebManageRoles_WebManageRoles1];
GO
IF OBJECT_ID(N'[dbo].[FK_WebManageUser_WebManageRoles_WebManageUsers1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WebManageUser_WebManageRoles] DROP CONSTRAINT [FK_WebManageUser_WebManageRoles_WebManageUsers1];
GO
IF OBJECT_ID(N'[dbo].[FK_WebManageUsers_WebDepartments1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[WebManageUsers] DROP CONSTRAINT [FK_WebManageUsers_WebDepartments1];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Order_Detail]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Order_Detail];
GO
IF OBJECT_ID(N'[dbo].[Order_Info]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Order_Info];
GO
IF OBJECT_ID(N'[dbo].[Product]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Product];
GO
IF OBJECT_ID(N'[dbo].[ProductDetail]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProductDetail];
GO
IF OBJECT_ID(N'[dbo].[User_Info]', 'U') IS NOT NULL
    DROP TABLE [dbo].[User_Info];
GO
IF OBJECT_ID(N'[dbo].[WebDepartments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WebDepartments];
GO
IF OBJECT_ID(N'[dbo].[WebManageMenus]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WebManageMenus];
GO
IF OBJECT_ID(N'[dbo].[WebManagePermissions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WebManagePermissions];
GO
IF OBJECT_ID(N'[dbo].[WebManageRole_WebManageMenus]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WebManageRole_WebManageMenus];
GO
IF OBJECT_ID(N'[dbo].[WebManageRoles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WebManageRoles];
GO
IF OBJECT_ID(N'[dbo].[WebManageUser_WebManageRoles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WebManageUser_WebManageRoles];
GO
IF OBJECT_ID(N'[dbo].[WebManageUsers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WebManageUsers];
GO
IF OBJECT_ID(N'[dbo].[WebSystem]', 'U') IS NOT NULL
    DROP TABLE [dbo].[WebSystem];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Order_Detail'
CREATE TABLE [dbo].[Order_Detail] (
    [OrderDetailID] int IDENTITY(1,1) NOT NULL,
    [OrderID] int  NOT NULL,
    [ProductID] int  NOT NULL,
    [ProductName] varchar(50)  NULL
);
GO

-- Creating table 'Order_Info'
CREATE TABLE [dbo].[Order_Info] (
    [OrderID] int IDENTITY(1,1) NOT NULL,
    [UserID] int  NOT NULL,
    [CreateDate] datetime  NOT NULL
);
GO

-- Creating table 'Product'
CREATE TABLE [dbo].[Product] (
    [ProductID] int IDENTITY(1,1) NOT NULL,
    [ProductName] varchar(50)  NOT NULL,
    [CreateDate] datetime  NOT NULL,
    [SaleCount] int  NOT NULL,
    [ClickCount] int  NULL,
    [Info] varchar(50)  NOT NULL,
    [UserID] int  NOT NULL,
    [SalePrice] decimal(18,0)  NOT NULL,
    [Discount] int  NOT NULL,
    [UpdateDate] datetime  NOT NULL
);
GO

-- Creating table 'ProductDetail'
CREATE TABLE [dbo].[ProductDetail] (
    [ProductDetailID] int IDENTITY(1,1) NOT NULL,
    [ProductID] int  NOT NULL,
    [ExtensionInfo] nvarchar(max)  NULL
);
GO

-- Creating table 'User_Info'
CREATE TABLE [dbo].[User_Info] (
    [UserID] int IDENTITY(1,1) NOT NULL,
    [UserName] varchar(50)  NULL,
    [RealName] varchar(50)  NULL,
    [ProductCount] int  NOT NULL,
    [Email] nvarchar(255)  NULL,
    [OrderCount] int  NOT NULL,
    [Phone] varchar(50)  NULL,
    [Address] nvarchar(512)  NULL,
    [CreateDate] datetime  NOT NULL,
    [UpdateDate] datetime  NOT NULL,
    [Status] int  NOT NULL
);
GO

-- Creating table 'WebDepartments'
CREATE TABLE [dbo].[WebDepartments] (
    [DepartmentID] int IDENTITY(1,1) NOT NULL,
    [DepartmentName] varchar(50)  NOT NULL,
    [About] varchar(500)  NOT NULL,
    [ParentID] int  NULL,
    [SortNumber] int  NOT NULL,
    [UpdateDate] datetime  NOT NULL,
    [Operator] varchar(50)  NULL,
    [Status] int  NOT NULL
);
GO

-- Creating table 'WebManageMenus'
CREATE TABLE [dbo].[WebManageMenus] (
    [MenuID] int IDENTITY(1,1) NOT NULL,
    [MenuName] varchar(50)  NOT NULL,
    [ParentID] int  NULL,
    [MenuLevel] int  NOT NULL,
    [LinkUrl] varchar(300)  NULL,
    [About] varchar(200)  NOT NULL,
    [SortNumber] int  NOT NULL,
    [UpdateDate] datetime  NOT NULL,
    [Operator] varchar(50)  NULL,
    [Status] int  NOT NULL
);
GO

-- Creating table 'WebManagePermissions'
CREATE TABLE [dbo].[WebManagePermissions] (
    [PermissionsID] int  NOT NULL,
    [Name] varchar(100)  NOT NULL,
    [Status] int  NOT NULL,
    [CreateDate] datetime  NOT NULL,
    [UpdateDate] datetime  NOT NULL
);
GO

-- Creating table 'WebManageRoles'
CREATE TABLE [dbo].[WebManageRoles] (
    [ManageRoleID] int IDENTITY(1,1) NOT NULL,
    [RoleName] varchar(50)  NOT NULL,
    [DepartmentID] int  NOT NULL,
    [About] varchar(200)  NOT NULL,
    [UpdateDate] datetime  NOT NULL,
    [SortNumber] int  NOT NULL,
    [Operator] varchar(50)  NOT NULL,
    [Status] int  NOT NULL,
    [OperatorAuthority] int  NOT NULL
);
GO

-- Creating table 'WebManageUsers'
CREATE TABLE [dbo].[WebManageUsers] (
    [ManageUserID] int IDENTITY(1,1) NOT NULL,
    [LoginName] varchar(20)  NOT NULL,
    [Password] varchar(50)  NOT NULL,
    [RealName] varchar(20)  NOT NULL,
    [Mobile] varchar(20)  NULL,
    [Email] varchar(50)  NULL,
    [Description] nvarchar(max)  NULL,
    [DepartmentID] int  NOT NULL,
    [CreateDate] datetime  NOT NULL,
    [UpdateDate] datetime  NOT NULL,
    [Operator] varchar(50)  NULL,
    [Status] int  NOT NULL,
    [WebSystemID] int  NULL
);
GO

-- Creating table 'WebSystem'
CREATE TABLE [dbo].[WebSystem] (
    [WebSystemID] int  NOT NULL,
    [WebSystemName] nvarchar(128)  NOT NULL,
    [Info] nvarchar(max)  NULL,
    [Status] int  NOT NULL
);
GO

-- Creating table 'WebManageRole_WebManageMenus'
CREATE TABLE [dbo].[WebManageRole_WebManageMenus] (
    [WebManageMenus_MenuID] int  NOT NULL,
    [WebManageRoles_ManageRoleID] int  NOT NULL
);
GO

-- Creating table 'WebManageUser_WebManageRoles'
CREATE TABLE [dbo].[WebManageUser_WebManageRoles] (
    [WebManageRoles_ManageRoleID] int  NOT NULL,
    [WebManageUsers_ManageUserID] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [OrderDetailID] in table 'Order_Detail'
ALTER TABLE [dbo].[Order_Detail]
ADD CONSTRAINT [PK_Order_Detail]
    PRIMARY KEY CLUSTERED ([OrderDetailID] ASC);
GO

-- Creating primary key on [OrderID] in table 'Order_Info'
ALTER TABLE [dbo].[Order_Info]
ADD CONSTRAINT [PK_Order_Info]
    PRIMARY KEY CLUSTERED ([OrderID] ASC);
GO

-- Creating primary key on [ProductID] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [PK_Product]
    PRIMARY KEY CLUSTERED ([ProductID] ASC);
GO

-- Creating primary key on [ProductDetailID] in table 'ProductDetail'
ALTER TABLE [dbo].[ProductDetail]
ADD CONSTRAINT [PK_ProductDetail]
    PRIMARY KEY CLUSTERED ([ProductDetailID] ASC);
GO

-- Creating primary key on [UserID] in table 'User_Info'
ALTER TABLE [dbo].[User_Info]
ADD CONSTRAINT [PK_User_Info]
    PRIMARY KEY CLUSTERED ([UserID] ASC);
GO

-- Creating primary key on [DepartmentID] in table 'WebDepartments'
ALTER TABLE [dbo].[WebDepartments]
ADD CONSTRAINT [PK_WebDepartments]
    PRIMARY KEY CLUSTERED ([DepartmentID] ASC);
GO

-- Creating primary key on [MenuID] in table 'WebManageMenus'
ALTER TABLE [dbo].[WebManageMenus]
ADD CONSTRAINT [PK_WebManageMenus]
    PRIMARY KEY CLUSTERED ([MenuID] ASC);
GO

-- Creating primary key on [PermissionsID] in table 'WebManagePermissions'
ALTER TABLE [dbo].[WebManagePermissions]
ADD CONSTRAINT [PK_WebManagePermissions]
    PRIMARY KEY CLUSTERED ([PermissionsID] ASC);
GO

-- Creating primary key on [ManageRoleID] in table 'WebManageRoles'
ALTER TABLE [dbo].[WebManageRoles]
ADD CONSTRAINT [PK_WebManageRoles]
    PRIMARY KEY CLUSTERED ([ManageRoleID] ASC);
GO

-- Creating primary key on [ManageUserID] in table 'WebManageUsers'
ALTER TABLE [dbo].[WebManageUsers]
ADD CONSTRAINT [PK_WebManageUsers]
    PRIMARY KEY CLUSTERED ([ManageUserID] ASC);
GO

-- Creating primary key on [WebSystemID] in table 'WebSystem'
ALTER TABLE [dbo].[WebSystem]
ADD CONSTRAINT [PK_WebSystem]
    PRIMARY KEY CLUSTERED ([WebSystemID] ASC);
GO

-- Creating primary key on [WebManageMenus_MenuID], [WebManageRoles_ManageRoleID] in table 'WebManageRole_WebManageMenus'
ALTER TABLE [dbo].[WebManageRole_WebManageMenus]
ADD CONSTRAINT [PK_WebManageRole_WebManageMenus]
    PRIMARY KEY NONCLUSTERED ([WebManageMenus_MenuID], [WebManageRoles_ManageRoleID] ASC);
GO

-- Creating primary key on [WebManageRoles_ManageRoleID], [WebManageUsers_ManageUserID] in table 'WebManageUser_WebManageRoles'
ALTER TABLE [dbo].[WebManageUser_WebManageRoles]
ADD CONSTRAINT [PK_WebManageUser_WebManageRoles]
    PRIMARY KEY NONCLUSTERED ([WebManageRoles_ManageRoleID], [WebManageUsers_ManageUserID] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [OrderID] in table 'Order_Detail'
ALTER TABLE [dbo].[Order_Detail]
ADD CONSTRAINT [FK_Order_Detail_Order_Info]
    FOREIGN KEY ([OrderID])
    REFERENCES [dbo].[Order_Info]
        ([OrderID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Order_Detail_Order_Info'
CREATE INDEX [IX_FK_Order_Detail_Order_Info]
ON [dbo].[Order_Detail]
    ([OrderID]);
GO

-- Creating foreign key on [UserID] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [FK_Product_User_Info]
    FOREIGN KEY ([UserID])
    REFERENCES [dbo].[User_Info]
        ([UserID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Product_User_Info'
CREATE INDEX [IX_FK_Product_User_Info]
ON [dbo].[Product]
    ([UserID]);
GO

-- Creating foreign key on [ProductID] in table 'ProductDetail'
ALTER TABLE [dbo].[ProductDetail]
ADD CONSTRAINT [FK_ProductDetail_Product]
    FOREIGN KEY ([ProductID])
    REFERENCES [dbo].[Product]
        ([ProductID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductDetail_Product'
CREATE INDEX [IX_FK_ProductDetail_Product]
ON [dbo].[ProductDetail]
    ([ProductID]);
GO

-- Creating foreign key on [ParentID] in table 'WebDepartments'
ALTER TABLE [dbo].[WebDepartments]
ADD CONSTRAINT [FK_WebDepartments_WebDepartments]
    FOREIGN KEY ([ParentID])
    REFERENCES [dbo].[WebDepartments]
        ([DepartmentID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WebDepartments_WebDepartments'
CREATE INDEX [IX_FK_WebDepartments_WebDepartments]
ON [dbo].[WebDepartments]
    ([ParentID]);
GO

-- Creating foreign key on [DepartmentID] in table 'WebManageRoles'
ALTER TABLE [dbo].[WebManageRoles]
ADD CONSTRAINT [FK_WebManageRoles_WebDepartments1]
    FOREIGN KEY ([DepartmentID])
    REFERENCES [dbo].[WebDepartments]
        ([DepartmentID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WebManageRoles_WebDepartments1'
CREATE INDEX [IX_FK_WebManageRoles_WebDepartments1]
ON [dbo].[WebManageRoles]
    ([DepartmentID]);
GO

-- Creating foreign key on [DepartmentID] in table 'WebManageUsers'
ALTER TABLE [dbo].[WebManageUsers]
ADD CONSTRAINT [FK_WebManageUsers_WebDepartments1]
    FOREIGN KEY ([DepartmentID])
    REFERENCES [dbo].[WebDepartments]
        ([DepartmentID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WebManageUsers_WebDepartments1'
CREATE INDEX [IX_FK_WebManageUsers_WebDepartments1]
ON [dbo].[WebManageUsers]
    ([DepartmentID]);
GO

-- Creating foreign key on [ParentID] in table 'WebManageMenus'
ALTER TABLE [dbo].[WebManageMenus]
ADD CONSTRAINT [FK_WebManageMenus_WebManageMenus]
    FOREIGN KEY ([ParentID])
    REFERENCES [dbo].[WebManageMenus]
        ([MenuID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WebManageMenus_WebManageMenus'
CREATE INDEX [IX_FK_WebManageMenus_WebManageMenus]
ON [dbo].[WebManageMenus]
    ([ParentID]);
GO

-- Creating foreign key on [WebManageMenus_MenuID] in table 'WebManageRole_WebManageMenus'
ALTER TABLE [dbo].[WebManageRole_WebManageMenus]
ADD CONSTRAINT [FK_WebManageRole_WebManageMenus_WebManageMenus]
    FOREIGN KEY ([WebManageMenus_MenuID])
    REFERENCES [dbo].[WebManageMenus]
        ([MenuID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [WebManageRoles_ManageRoleID] in table 'WebManageRole_WebManageMenus'
ALTER TABLE [dbo].[WebManageRole_WebManageMenus]
ADD CONSTRAINT [FK_WebManageRole_WebManageMenus_WebManageRoles]
    FOREIGN KEY ([WebManageRoles_ManageRoleID])
    REFERENCES [dbo].[WebManageRoles]
        ([ManageRoleID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WebManageRole_WebManageMenus_WebManageRoles'
CREATE INDEX [IX_FK_WebManageRole_WebManageMenus_WebManageRoles]
ON [dbo].[WebManageRole_WebManageMenus]
    ([WebManageRoles_ManageRoleID]);
GO

-- Creating foreign key on [WebManageRoles_ManageRoleID] in table 'WebManageUser_WebManageRoles'
ALTER TABLE [dbo].[WebManageUser_WebManageRoles]
ADD CONSTRAINT [FK_WebManageUser_WebManageRoles_WebManageRoles]
    FOREIGN KEY ([WebManageRoles_ManageRoleID])
    REFERENCES [dbo].[WebManageRoles]
        ([ManageRoleID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [WebManageUsers_ManageUserID] in table 'WebManageUser_WebManageRoles'
ALTER TABLE [dbo].[WebManageUser_WebManageRoles]
ADD CONSTRAINT [FK_WebManageUser_WebManageRoles_WebManageUsers]
    FOREIGN KEY ([WebManageUsers_ManageUserID])
    REFERENCES [dbo].[WebManageUsers]
        ([ManageUserID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_WebManageUser_WebManageRoles_WebManageUsers'
CREATE INDEX [IX_FK_WebManageUser_WebManageRoles_WebManageUsers]
ON [dbo].[WebManageUser_WebManageRoles]
    ([WebManageUsers_ManageUserID]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------